/**
 * 打开问卷编辑界面
 * 
 * @param v
 */
function openNairePage(v) {
	var _projectId = $("#_projectId").val();
	var url = "openNairePage.do?naire.projectId=" + _projectId;
	if (v) {
		url += "&naire.naireId=" + v;
	}
	$("#naireDialog").dialog("setTitle", '编辑问卷').dialog('open').dialog(
			'refresh', url);
}

/**
 * 保存问卷
 */
function submitNaire() {
	$('#naireForm').form('submit', {
		url : 'editNaire.do',
		onSubmit : function() {
			return true;
		},
		success : function(data) {
			if (data == "success") {
				$.messager.alert('成功', '保存成功！', 'info', function() {
					$('#naireDialog').dialog('close');
					$('#naireList').datagrid('reload');
				});
			} else if (data == "login") {
				window.location.href = 'index.jsp';
			}
		}
	});
}

/**
 * 生成问卷
 */
function loadNaireTemplate(newValue) {
	var _projectId = $("#naire_projectId").val();
	var naireId = $("#naire_naireId").val();
	var values = {};
	if (naireId != "") {
		values = {
			"naire.naireId" : naireId
		};
	} else {
		values = {
			"naire.projectId" : _projectId,
			"naire.chkOrgId" : newValue
		};
	}
	var url = "findNaireContent.do?timestamp=" + new Date();
	$.post(url, values, function(data) {
		$("#loadNaireTemplate").append(
				data.replace(/@F@/g, "<input type='text'>"));
	});
}

/**
 * 问卷试题得分
 * 
 * @param qId
 * @param val
 */
function score(qId, val) {
	var vs = $("#vs" + qId).val();
	var xmlDoc = loadXML(vs);
	var qsElement = xmlDoc.getElementsByTagName("source")[0];
	var x = qsElement.childNodes[0];
	if (x) {
		qsElement.removeChild(x);
	}
	qsText = xmlDoc.createTextNode(val);
	qsElement.appendChild(qsText);
	var text = "";
	if (document.all) {
		text = xmlDoc.xml;
	} else {
		// Firefox,Chrome等其它浏览器操作XML
		text = (new XMLSerializer()).serializeToString(xmlDoc);

	}
	$("#vs" + qId).val(text);
}

/**
 * 
 * 
 * @param qId
 * @param val
 */
function fill(qId, val) {
	var vs = $("#vs" + qId).val();
	var xmlDoc = loadXML(vs);
	var qsElement = xmlDoc.getElementsByTagName("questionText")[0];
	var x = qsElement.childNodes[0];
	if (x) {
		qsElement.removeChild(x);
	}
	qsText = xmlDoc.createTextNode(val);
	qsElement.appendChild(qsText);
	var text = "";
	if (document.all) {
		text = xmlDoc.xml;
	} else {
		// Firefox,Chrome等其它浏览器操作XML
		text = (new XMLSerializer()).serializeToString(xmlDoc);

	}
	$("#vs" + qId).val(text);
}

/**
 * 问卷选项
 * 
 * @param qId
 * @param oId
 * @param tp
 */
function selectOption(qId, oId, tp) {
	var vs = $("#vs" + qId).val();
	var xmlDoc = loadXML(vs);
	// Firefox,Chrome等其它浏览器操作XML
	var qsElement = xmlDoc.getElementsByTagName("option");
	if (tp == 'r') {
		for ( var i = 0; i < qsElement.length; i++) {
			var n = qsElement[i];
			var attrId = n.getAttribute("id");
			var o = document.getElementById(qId + "_" + attrId);
			o.checked = false;
			n.setAttribute("selected", "N");
			if (attrId == oId) {
				o.checked = true;
				n.setAttribute("selected", "Y");
			}
		}
	} else if (tp == 'c') {
		for ( var i = 0; i < qsElement.length; i++) {
			var n = qsElement[i];
			var attrId = n.getAttribute("id");
			if (attrId == oId) {
				var o = document.getElementById(qId + "_" + attrId);
				if (o.checked) {
					$("#" + qId + "_" + attrId).checked = true;
					n.setAttribute("selected", "Y");
				} else {
					$("#" + qId + "_" + attrId).checked = false;
					n.setAttribute("selected", "N");
				}
			}
		}
	}
	if (document.all) {
		$("#vs" + qId).val(xmlDoc.xml);
	} else {
		var text = (new XMLSerializer()).serializeToString(xmlDoc);
		$("#vs" + qId).val(text);
	}
}

/**
 * 考核指标，使用
 * 
 * @param qId
 * @param val
 */
function changeUse(qId, val) {
	var vs = $("#vs" + qId).val();
	var xmlDoc = loadXML(vs);
	var qsElement = xmlDoc.getElementsByTagName("use")[0];
	var x = qsElement.childNodes[0];
	if (x) {
		qsElement.removeChild(x);
	}
	qsText = xmlDoc.createTextNode(val);
	qsElement.appendChild(qsText);
	var text = "";
	if (document.all) {
		text = xmlDoc.xml;
	} else {
		// Firefox,Chrome等其它浏览器操作XML
		text = (new XMLSerializer()).serializeToString(xmlDoc);

	}
	$("#vs" + qId).val(text);
}

/**
 * 问卷作废
 */
function invalidNaire() {
	var r = $("#naireList").datagrid("getSelected");
	if (r == null) {
		$.messager.alert('提示', '请选择！', 'info');
		return;
	}
	var url = "invalidNaire.do";
	$.post(url, {
		"naire.naireId" : r.naireId
	}, function(data) {
		if (data == "success") {
			$.messager.alert('提示', '操作成功！', 'info', function() {
				$('#naireDialog').dialog('close');
				$('#naireList').datagrid('reload');
				$('#naireList').datagrid('clearSelections');
			});
		} else if (data == "login") {
			window.location.href = 'index.jsp';
		}
	});
}

/**
 * 一审
 */
function firstAut() {
	var r = $("#naireList").datagrid("getSelected");
	if (r == null) {
		$.messager.alert('提示', '请选择！', 'info');
		return;
	}
	var url = "firstAutNaire.do";
	$.post(url, {
		"naire.naireId" : r.naireId
	}, function(data) {
		if (data == "success") {
			$.messager.alert('提示', '操作成功！', 'info', function() {
				$('#naireDialog').dialog('close');
				$('#naireList').datagrid('reload');
				$('#naireList').datagrid('clearSelections');
			});
		} else if (data == "login") {
			window.location.href = 'index.jsp';
		}
	});
}

/**
 * 二审
 */
function secondAut() {
	var r = $("#naireList").datagrid("getSelected");
	if (r == null) {
		$.messager.alert('提示', '请选择！', 'info');
		return;
	}
	var url = "secondAutNaire.do";
	$.post(url, {
		"naire.naireId" : r.naireId
	}, function(data) {
		if (data == "success") {
			$.messager.alert('提示', '操作成功！', 'info', function() {
				$('#naireDialog').dialog('close');
				$('#naireList').datagrid('reload');
				$('#naireList').datagrid('clearSelections');
			});
		} else if (data == "login") {
			window.location.href = 'index.jsp';
		}
	});
}

/**
 * 调研问卷前三次提示
 * 
 * @param id
 */
function tipsAlert(id, o) {
	var tips = $("#tips" + id).html();
	$.messager.show({
		title:'提示',
		msg:tips,
		timeout:5000,
		showType:'slide'
	});
}